package com.appspot.abcensys.shared;

import java.util.ArrayList;

import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import com.google.gwt.user.client.rpc.IsSerializable;

@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class Gebruiker implements IsSerializable {
	
	@SuppressWarnings("unused")
	private static final long serialVersionUID = 1L;

	public static final Integer TYPE_DOCENT = 0;
	public static final Integer TYPE_STUDENT = 1;
	public static final Integer TYPE_GAST = 2;
	public static final Integer TYPE_TEAMLEIDER = 3;
	public static final Integer TYPE_MANAGER_BEDRIJFSVOERING = 4;

	// Gebruiker
	/* Inheritance in GAE is a B*TCH! */
	@Persistent
	@PrimaryKey
	private String email;
	@Persistent
	private Integer type;
	@Persistent
	private String voornaam;
	@Persistent
	private String achternaam;
	@Persistent
	private Integer isAdministrator;

	// Docent
	@Persistent
	private String docentCode;
	@Persistent
	private ArrayList<String> vakCodes;

	// Student
	@Persistent
	private String studentNummer;
	@Persistent
	private String faculteit;
	@Persistent
	private String opleiding;
	@Persistent
	private String klasCode;

	public void fix() {
		ArrayList<String> fixed = new ArrayList<String>();
		for (String s : this.vakCodes) {
			fixed.add(s);
		}
		this.vakCodes = fixed;
	}

	/**
	 * constructor
	 */
	public Gebruiker() {
		this.vakCodes = new ArrayList<String>();
		this.type = Gebruiker.TYPE_GAST;
		this.setIsAdministrator(false);
	}

	/**
	 * Constructor
	 * 
	 * @param email
	 */
	public Gebruiker(String email) {
		this.email = email.toLowerCase().trim();
		this.vakCodes = new ArrayList<String>();
		this.type = Gebruiker.TYPE_GAST;
		this.setIsAdministrator(false);
	}

	/**
	 * setType
	 * 
	 * @param int Type gebruiker
	 */
	public void setType(int type) {
		this.type = type;
	}

	/**
	 * 
	 * @return
	 */
	public int getType() {
		return this.type;
	}

	/**
	 * @param email
	 */
	public void setEmail(String email) {
		this.email = email.toLowerCase().trim();
	}

	/**
	 * Fetch the users email address
	 * 
	 * @return the users email address
	 */

	public String getEmail() {
		if (this.email != null)
			return this.email.toLowerCase().trim();
		return this.email;
	}

	/**
	 * @return the voornaam
	 */
	public String getVoornaam() {
		return voornaam;
	}

	/**
	 * @param voornaam
	 *            the voornaam to set
	 */
	public void setVoornaam(String voornaam) {
		this.voornaam = voornaam;
	}

	/**
	 * @return the achternaam
	 */
	public String getAchternaam() {
		return achternaam;
	}

	/**
	 * @param achternaam
	 *            the achternaam to set
	 */
	public void setAchternaam(String achternaam) {
		this.achternaam = achternaam;
	}

	/**
	 * @return the docentCode
	 */
	public String getDocentCode() {
		return docentCode;
	}

	/**
	 * @param docentCode
	 *            the docentCode to set
	 */

	public void setDocentCode(String docentCode) {
		this.docentCode = docentCode;
	}

	/**
	 * @return the deVakken
	 */

	public ArrayList<String> getVakCodes() {
		return vakCodes;
	}

	/**
	 * @param deVakken
	 *            the deVakken to set
	 */

	public void setVakCodes(ArrayList<String> vakCodes) {
		this.vakCodes = vakCodes;
	}

	/**
	 * 
	 * @param vak
	 */

	public void setVakCode(String vakCode) {
		this.vakCodes.add(vakCode);
	}

	/**
	 * @param vakCode
	 * @return gezochte Vak
	 */

	public String zoekVak(String vakCode) {
		for (String vakcode : vakCodes) {
			if (vakcode.equals(vakCode)) {
				return vakcode;
			}
		}
		return null;
	}

	/**
	 * @return the studentNummer
	 */
	public String getStudentNummer() {
		return studentNummer;
	}

	/**
	 * @param studentNummer
	 *            the studentNummer to set
	 */
	public void setStudentNummer(String studentNummer) {
		this.studentNummer = studentNummer;
	}

	/**
	 * @return the faculteit
	 */
	public String getFaculteit() {
		return faculteit;
	}

	/**
	 * @param faculteit
	 *            the faculteit to set
	 */
	public void setFaculteit(String faculteit) {
		this.faculteit = faculteit;
	}

	/**
	 * @return the opleiding
	 */
	public String getOpleiding() {
		return opleiding;
	}

	/**
	 * @param opleiding
	 *            the opleiding to set
	 */
	public void setOpleiding(String opleiding) {
		this.opleiding = opleiding;
	}

	/**
	 * @return the klasCode
	 */
	public String getKlasCode() {
		return klasCode;
	}

	/**
	 * @param klasCode
	 *            the klasCode to set
	 */
	public void setKlasCode(String klasCode) {
		this.klasCode = klasCode;
	}

	/**
	 * @param isAdministrator
	 *            the isAdministrator to set
	 */
	public void setIsAdministrator(Boolean isAdministrator) {
		this.isAdministrator = (isAdministrator == true ? 1 : 0);
	}

	/**
	 * @return the isAdministrator
	 */
	public Boolean getIsAdministrator() {
		if (this.isAdministrator == 1) {
			return true;
		}
		return false;
	}
}
